// pages/consultationOnline/index.js
const { getWeek, getTodayDate, formatNumber } = require('./../../utils/util')
const api = require('./../../config/api')
const request = require('./../../utils/request')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    diarylist: [],
    tilast: '',
    height: '',
    page: 1,
    size: 10,
    totalPage: 0, // 总页数
    weekDayList: [],
    taskDayList: [],
    currentYear: '',
    currentMonth: '',
    currentDay: '',
    currentWeek: '',
    yearMonth: '', // 年-月
    isLoading: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    const today = getTodayDate()
    const date = today.year + '-' + today.month + '-' + today.day
    this.setData({
      height: wx.getSystemInfoSync().windowHeight,
      currentYear: today.year,
      currentMonth: formatNumber(today.month),
      currentDay: date,
      currentWeek: getWeek(date).week,
      yearMonth: today.year + '-' + formatNumber(today.month),
    })
  },
  //获取咨询分类列表
  getDiaryList: async function () {
    let { page, size, diarylist, yearMonth } = this.data
    const patientInfo = wx.getStorageSync('patientInfo')
    const res = await request.get(
      api.diaryList,
      {
        patientId: patientInfo.id,
        page,
        size,
        yearMonth: yearMonth,
      },
      { loading: false }
    )
    if (res.httpStatus === 200) {
      res.data.records.map((item) => {
        const time = item.releaseTime.split(' ')
        item.releaseTime = time[0]
        item.weekday = getWeek(item.releaseTime)
        item.time = time[1].substring(0, 5)
        return item
      })
      this.setData({
        diarylist: [...diarylist, ...res.data.records],
        totalPage: res.data.pages,
        isLoading: false,
      })
    }
    wx.hideLoading()
  },
  /**
   * 日期选中变化
   * @param {*} e
   */
  change(e) {
    this.setData({
      yearMonth: e.detail,
      page: 1,
      size: 10,
      totalPage: 0,
      diarylist: [],
      isLoading: true,
    })
    this.getDiaryList()
  },
  createDiary() {
    wx.navigateTo({
      url: `/pages/diaryCreate/index`,
    })
  },
  detail(val) {
    wx.navigateTo({
      url: `/pages/diaryDetail/index?diaryId=${val.currentTarget.dataset.id}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.calendarDiary = this.selectComponent('#calendarDiary')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.calendarDiary) {
      this.calendarDiary.dateInit()
    }
    this.setData({
      page: 1,
      size: 10,
      totalPage: 0,
      diarylist: [],
      isLoading: true,
    })
    this.getDiaryList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      diarylist: [],
      isLoading: true,
    })
    this.getDiaryList()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let { page, totalPage } = this.data
    if (page === totalPage) {
      return
    }
    page += 1
    this.setData({
      page: page,
      isLoading: true,
    })
    this.getDiaryList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
})
